{include file="common/head"/}
<script type="text/javascript" src="__STATIC__/common/js/jquery.2.1.1.min.js"></script>
<script src="__STATIC__/common/js/angular.min.js"></script>
<style>
    .layui-btn-xs{
    height: 22px;
    line-height: 22px;
    padding: 0 5px;
    font-size: 12px;}
    #tb-box{display: none;}
    #v-audio{
        width:300px;
        height: 40px;
    }
    .v-audio, .v-video{display: none}
</style>
<div class="admin-main layui-anim layui-anim-upbit" ng-app="hd" ng-controller="ctrl">

    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">选择章节</label>
            <div class="layui-input-4">
                <select name="pid" {if ($pid == 0) && ($edit == 'edit' )} disabled {/if}>
                    <option value="0" selected >默认顶级</option>
                    {volist name='catList' id='v'}
                        <option value="{$v.course_detail_id}" {in name="v.course_detail_id" value="$pid"} selected {/in} >{$v.name}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-4">
                <input type="text" name="name" ng-model="field.name" lay-verify="required" placeholder="{:lang('pleaseEnter')}课程名称" class="layui-input">
            </div>
        </div>

        <!-- 封面图片 -->
       <!-- <div class="layui-form-item">
            <label class="layui-form-label">上传图片</label>
            <input type="hidden" name="pic" id="pic" value="{{field.pic}}">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="adBtn"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="adPic">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>-->

        <div class="layui-form-item">
            <label class="layui-form-label">上传音频</label>
            <input type="hidden" name="aurl" id="audio" value="{{field.url}}">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="upAudio"><i class="layui-icon"></i>上传音频</button>
                    {if ($infos['type'] == 2) && ($infos['url'] != '') }
                        <div class="layui-upload-list">
                            <video controls="" autostart="false" id="v-audio"  name="media"><source src="{$infos.url}" type="audio/mpeg"></video>
                        </div>
                    {else/}
                        <div class="layui-upload-list v-audio">
                            <video controls="" autostart="false" id="v-audio"  name="media"><source src="" type="audio/mpeg"></video>
                        </div>
                    {/if}
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">上传视频</label>
            <input type="hidden" name="vurl" id="video" value="{{field.url}}">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="upVideo"><i class="layui-icon"></i>上传视频</button>
                    {if ($infos['type'] == 1) && ($infos['url'] != '')}
                        <div class="layui-upload-list">
                            <video controls="" autostart="false"  name="media"><source src="{$infos.url}" type="video/mp4"></video>
                        </div>
                    {else/}
                        <div class="layui-upload-list v-video">
                            <video controls="" autostart="false"  name="media"><source src="" type="video/mp4"></video>
                        </div>
                    {/if}
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">时长(秒)</label>
            <div class="layui-input-4">
                <input type="text" name="times" ng-model="field.times"  placeholder="{:lang('pleaseEnter')}课程名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" ng-model="field.status" ng-checked="field.status==1" ng-value="1" title="正常">
                <input type="radio" name="status" ng-model="field.status" ng-checked="field.status==0" ng-value="0" title="隐藏">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" ng-model="field.type" ng-checked="field.type==1" ng-value="1" title="视频">
                <input type="radio" name="type" ng-model="field.type" ng-checked="field.type==2" ng-value="2" title="音频">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否试听</label>
            <div class="layui-input-block">
                <input type="radio" name="is_preview" ng-model="field.is_preview" ng-checked="field.is_preview==1" ng-value="1" title="可以">
                <input type="radio" name="is_preview" ng-model="field.is_preview" ng-checked="field.is_preview==0" ng-value="0" title="不可以">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('order')}</label>
            <div class="layui-input-2">
                <input type="text" name="sort" ng-model="field.sort" value="" placeholder="从小到大排序" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="course_id" value="{$course_id}">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>

    function fromAtion(action,id){
       if(id == ''){
           layer.alert('请先添加课程!');
           return false;
       }else{

           var url = action+"?course_id="+id+"&long=0";
           location.href = "{:url('"+url+"')}";
       }
    }

    var m = angular.module('hd',[]);
    m.controller('ctrl',['$scope',function($scope) {
        $scope.field = '{$info}'!='null'?{$info}:{sort:0,status:0,type:1,is_preview:0};
        console.log($scope.field);
        layui.use(['form', 'layer','upload','layedit','table'], function () {
            var form = layui.form, $ = layui.jquery,layedit = layui.layedit, upload = layui.upload,table = layui.table;
            if($scope.field.pic){
                adPic.src = "__PUBLIC__"+ $scope.field.pic;
            }

            //富文本编辑图片上传操作
            layedit.set({
                uploadImage: {
                    url: '{:url("UpFiles/editUpload")}' //接口url
                }
            });

            //设定一个富文本编辑器
            var index = layedit.build('content'); //建立编辑器


            //表单提交操作
            form.on('submit(submit)', function (data) {
                // 提交到方法 默认为本身
                //data.field.content = layedit.getContent(index);  //获取富文本内容
                data.field.course_id = {$course_id};
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("", data.field, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        var index = parent.layer.getFrameIndex(window.name);
                        layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                            parent.loades(res.url);
                            parent.layer.close(index);
                        });
                    } else {
                        var index = parent.layer.getFrameIndex(window.name);
                        layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                            parent.loades(res.url);
                            parent.layer.close(index);
                        });
                        //layer.msg(res.msg, {time: 1800, icon: 2});
                    }
                });
            });

            //选择栏目类型
            $('#getCat').click(function(){
                layer.open({
                    type: 2,
                    title: '选择项目类型',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['70%', '80%'],
                    content: "__URL__/getCat"
                });
            });

            //普通图片上传
            /*var uploadInst = upload.render({
                elem: '#adBtn'
                ,url: '{:url("upQiniu")}'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#adPic').attr('src', result); //图片链接（base64）
                    });
                },
                done: function(res){
                    if(res.code>0){
                        $('#pic').val(res.url);
                        layer.msg('图片上传成功');
                    }else{
                        //如果上传失败
                        return layer.msg('图片上传成功');
                    }
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });
*/

            //上传音频类型
            upload.render({
                elem: '#upAudio'
                ,url: '{:url("upQiniu")}'
                ,accept: 'audio' //音频
                ,exts:'mp3'
                ,size: 51200
                ,before: function(obj){
                    //加载层
                    var index = layer.load(0, {shade: false});
                }
                ,done: function(res){
                    if(res.code>0){
                        layer.close(index);  //关闭加载弹框
                        $('#audio').val(res.url);
                        layer.msg('音频上传成功');

                        $('.v-audio').css('display','block').find('source').attr('src',res.url)
                    }else{
                        layer.close(index);  //关闭加载弹框
                        //如果上传失败
                        return layer.msg('音频上传成功');
                    }
                }
            });


            //上传视频
            upload.render({
                elem: '#upVideo'
                ,url: '{:url("upQiniu")}'
                ,accept: 'video' //视频
                ,exts:'mp4|flv'
                ,size: 51200
                ,before: function(obj){
                    //加载层
                    var index = layer.load(0, {shade: false});
                }
                ,done: function(res){
                    if(res.code>0){
                        layer.close(index);  //关闭加载弹框
                        $('#video').val(res.url);
                        layer.msg('视频上传成功');

                        $('.v-video').css('display','block').find('source').attr('src',res.url)
                    }else{
                        layer.close(index);  //关闭加载弹框
                        //如果上传失败
                        return layer.msg('视频上传成功');
                    }
                }
            });

        });
    }]);
</script>
